// pages/hotel_list/index.js

const app=getApp();
Page({

  // 改变一级选择状态
  changeStatus: function (event) {
    if(this.data.currentIndex === event.currentTarget.dataset.index) {
      this.setData({
        currentIndex:null
      })
      return
    }
    this.setData({
      currentIndex:event.currentTarget.dataset.index
    })
  },
  // 封装函数1
  select_status: function (event) {
    let data = {}
     // 筛选
     if(event.currentTarget.dataset.idx === 0 && event.currentTarget.dataset.indexof === 1) {
       if(this.data.stars){
        data = {
          orderBy:event.currentTarget.dataset.indexof + 1,
          stars: this.data.stars,
          page:this.data.page
        }
       }else{
        data = {
          orderBy:event.currentTarget.dataset.indexof + 1,
          page:this.data.page
        }
       }
      this.send_data(data)
      this.setData({
        orderBy:event.currentTarget.dataset.indexof + 1
      })
      return
    }
    // 筛选
    if(event.currentTarget.dataset.idx === 0 && event.currentTarget.dataset.indexof === 2) {
      if(this.data.stars){
        data = {
          orderBy:event.currentTarget.dataset.indexof - 1,
          stars: this.data.stars,
          page:this.data.page
        }
       }else{
        data = {
          orderBy:event.currentTarget.dataset.indexof - 1,
          page:this.data.page
        }
       }
      this.send_data(data)
      this.setData({
        orderBy:event.currentTarget.dataset.indexof - 1
      })
      return
    }
        // 筛选
    if(event.currentTarget.dataset.idx === 0 && event.currentTarget.dataset.indexof === 0) {
      if(this.data.stars){
        data = {
          orderBy:event.currentTarget.dataset.indexof,
          stars: this.data.stars,
          page:this.data.page
        }
       }else{
        data = {
          orderBy:event.currentTarget.dataset.indexof,
          page:this.data.page
        }
       }
      this.send_data(data)
      this.setData({
        orderBy:event.currentTarget.dataset.indexof
      })
      return
    }
    // 筛选
    if(event.currentTarget.dataset.idx === 1 && event.currentTarget.dataset.indexof === 1) {
      data = {
        orderBy:this.data.orderBy,
        stars:3,
        page:this.data.page,
      }
      this.send_data(data)
      this.setData({
        stars:3
      })
      return
    }
    // 筛选
    if(event.currentTarget.dataset.idx === 1 && event.currentTarget.dataset.indexof === 2) {
      data = {
        orderBy:this.data.orderBy,
        stars:4,
        page:this.data.page,
      }
      this.send_data(data)
      this.setData({
        stars:4
      })
      return
    }
    // 筛选
    if(event.currentTarget.dataset.idx === 1 && event.currentTarget.dataset.indexof === 0) {
      data = {
        orderBy:this.data.orderBy,
        page:this.data.page,
      }
      this.send_data(data)
      this.setData({
        stars:null
      })
      return
    }
  },
  // 封装函数2 
  send_data: async function (data) {
    wx.showLoading({
      title: '加载中',
    })
    const result = await app.wxPost({
      url: '/mini/hotel/getdate.do',
      data:data
    })
    wx.hideLoading()
    this.setData({
      hotel_list: result.data.rows,
      MaxPage: result.data.maxPage,
      onclick: false
    })
    if(this.data.MaxPage === this.data.page) {
      this.setData({
        More: true
      })
    }
  },
    // 封装函数2 
    render_data: async function (data) {
      wx.showLoading({
        title: '加载中',
      })
      const result = await app.wxPost({
        url: '/mini/hotel/getdate.do',
        data:data
      })
      wx.hideLoading()
      this.setData({
        hotel_list: this.data.hotel_list.concat(result.data.rows),
        MaxPage: result.data.maxPage,
        loding:false
      })
      if(this.data.MaxPage === this.data.page) {
        this.setData({
          More: true,
        })
      }
    },
  // 下拉刷新函数
  onRefresh: function () {
    let data = {}
    if(this.data.stars){
      data = {
        orderBy:this.data.orderBy,
        stars: this.data.stars,
        page:this.data.page
      }
     }else{
      data = {
        orderBy:this.data.orderBy,
        page:this.data.page
      }
     }
     this.send_data(data)
     wx.stopPullDownRefresh()
     if(this.data.MaxPage === this.data.page) {
      this.setData({
        More: true
      })
    }
  },
   // 改变二级选择状态
  changeType: async function(event) {
    // 防止请求过多点击
    if(this.data.onclick) {
      return
    }
    var name = `navList[${event.currentTarget.dataset.idx}].name`
    var checkoutName = `checkout[${event.currentTarget.dataset.idx}]`
    this.setData({
      [name]:event.currentTarget.dataset.name,
      [checkoutName]: event.currentTarget.dataset.indexof,
      page: 1,
      onclick: true
    })
    this.select_status(event)
  },
  // 初始化函数
  initData: async function () {
    wx.showLoading({
      title: '加载中',
    })
    const result = await app.wxPost({
      url: '/mini/hotel/getdate.do',
      data:{
        orderBy:0,
        page:this.data.page
      }
    })
    wx.hideLoading()
    this.setData({
      hotel_list: result.data.rows,
      MaxPage: result.data.maxPage,
      onredely:true
    })
    if(this.data.MaxPage === this.data.page) {
      this.setData({
        More: true
      })
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: null,
    navList: [
    {name: '智能排序',children:[{name:'智能排序'},{name:'最新发布'},{name:'价格更低'}]},
    {name: '星级',children:[{name:'不限'},{name:'3星'},{name:'4星'}]},
    {name: '主题',children:[{name:'不限'}]}
   ],
  // 选中暂存
  checkout:[0,0,0],
  hotel_list: [],
  orderBy: 0,
  onredely: false, //初始化数据渲染
  stars:null,
  page:1, //页码,
  MaxPage:null, //最大页码,
  More: false,
  loding: false, //防止二次加载,
  onclick: false //防止二次点击

  // hotel_id: 34
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.More || this.data.loding) {
      return
    }
    //防止二次加载
    this.setData({
      loding: true
    })
    let data = {}
    this.setData({
      page: this.data.page + 1
    })
    if(this.data.stars){
      data = {
        orderBy:this.data.orderBy,
        stars: this.data.stars,
        page:1
      }
     }else{
      data = {
        orderBy:this.data.orderBy,
        page:1
      }
     }
     this.render_data(data)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})